﻿/// <reference path="../../utilities/FXDom.js">
///--------------------------------------------------------------------
/// <file name="AutoCompleteEntry.js">
///   Encapsulates a single entry in the AutoComplete UI.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.AutoCompleteEntry = function(entryText, splitIndex, description, entryTitle, transformer) {
    /// <summary>
    ///   Initializes a new instance of the AutoCompleteEntry class.
    /// </summary>
    /// <param name="entryText" type="String">
    ///   The text to put in the text area when this entry is chosen.
    /// </param>
    /// <param name="splitIndex" type="Number" integer="true">
    ///   The index at which selections should be inserted or where
    ///   the caret should be placed after the entryText has been
    ///   inserted. This is relative to the beginning of the entryText string.
    /// </param>
    /// <param name="description" type="String">
    ///   The description of the entryText tag, used to help users understand
    ///   the purpose of the entryText.
    /// </param>
    /// <param name="entryTitle" type="String" optional="true">
    ///   (Optional) The text shown in the AutoComplete box. The default
    ///   is the entryText itself.
    /// </param>
    /// <param name="transformer" type="Function">
    ///   The function that transforms selected text when this
    ///   AutoCompleteEntry is chosen.
    /// </param>
    
    this._entryText = entryText;
    this._entryTitle = entryText || entryTitle;
    this._splitIndex = splitIndex;
    this._description = description;
    this._transformer = transformer;
};

AutoCompleteEntry.selectItem = function(listItem) {
    /// <summary>
    /// Makes the specified list item appear as selected.
    /// </summary>
    /// <param name="listItem">The DOM element that represents an entry in AutoComplete.</param>

    listItem.className = "namfox-auto-complete-selected";
};

AutoCompleteEntry.suggestItem = function(listItem) {
    /// <summary>
    /// Suggests the specified list item as a match in AutoComplete.
    /// </summary>
    /// <param name="listItem">The DOM element that represents an entry in AutoComplete.</param>
    
    listItem.className = "namfox-auto-complete-suggested";
};

AutoCompleteEntry.unselectItem = function(listItem) {
    /// <summary>
    /// Makes the specified list item appear as unselected.
    /// </summary>
    /// <param name="listItem">The DOM element that represents an entry in AutoComplete.</param>

    listItem.className = null;
};

AutoCompleteEntry.prototype = {
    addAttributes: function(listItem) {
        /// <summary>
        ///   Adds attributes to the list item.
        /// </summary>
        /// <param name="listItem">
        ///   The list item element created for the auto-complete element.
        /// </param>
        

        // Default implementation is empty.
    },
    
    addTo: function(textArea, context) {
        /// <summary>
        /// Adds the text associated with this AutoCompleteEntry
        /// to the specified text area, taking into account the
        /// context (i.e. the text behind and ahead of the caret).
        /// </summary>
        /// <param name="textArea" type="TextArea">
        /// The text area to which to add the entry's text.
        /// </param>
        /// <param name="context">
        /// The textual context of the caret. Contains two properties,
        /// previous and following, that provide this information.
        /// </param>
        
        var entryText = this._entryText;
        var splitIndex = this._splitIndex;
        
        if (textArea.selectionStart === textArea.selectionEnd) {
            var selectionStart = textArea.selectionStart - context.previous.length;
            textArea.setSelectionRange(
                selectionStart,
                textArea.selectionEnd + context.following.length
            );
            
            var plainTextEditor = textArea.
                QueryInterface($("nsIDOMNSEditableElement")).
                editor.
                QueryInterface($("nsIPlaintextEditor"));
            
            plainTextEditor.insertText(entryText);
            
            selectionStart += splitIndex;
            textArea.setSelectionRange(selectionStart, selectionStart);
        }
        else {
            var text = {
                open: entryText.substring(0, splitIndex),
                close: entryText.substr(splitIndex)
            };
            Markup.addText(text, textArea, SelectionRule.SelectNewText, this._transformer);
        }
    },
        
    getDescription: function() {
        /// <summary>
        /// Gets the description associated with this AutoCompleteEntry.
        /// </summary>
        /// <returns type="String" />
        
        return this._description;
    },
    
    getText: function() {
        /// <summary>
        /// Gets the entry text associated with this AutoCompleteEntry.
        /// </summary>
        /// <returns type="String" />
        
        return this._entryText;
    },
    
    getTitle: function() {
        /// <summary>
        /// Gets the entry title associated with this AutoCompleteEntry.
        /// </summary>
        /// <returns type="String" />
        
        return this._entryTitle;
    }
};

}});